package com.thirtydays.frame.util.ext

import android.app.Activity
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_CALL
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import android.util.Pair
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.thirtydays.frame.app.MyApp
import com.thirtydays.frame.base.BaseApp
import java.io.Serializable


//获取资源
fun getCompatColor(@ColorRes id: Int) = ContextCompat.getColor(BaseApp.appContent, id)

//获取资源
fun getCompatDrawable(@DrawableRes id: Int) = ContextCompat.getDrawable(BaseApp.appContent, id)

fun getDimen(@DimenRes id: Int) = BaseApp.appContent.resources.getDimension(id)

//获取字符串
fun getCompatString(@StringRes id: Int) = BaseApp.appContent.getString(id)


inline fun <reified T : Activity> Activity.pop(vararg params: Pair<String, Any?>) {
    this.startActivity(createIntent(this, T::class.java, params))
}

inline fun <reified T : Activity> Fragment.pop(vararg params: Pair<String, Any?>) {
    this.startActivity(this.activity?.let { createIntent(it, T::class.java, params) })
}


fun <T> createIntent(ctx: Context, clazz: Class<out T>, params: Array<out Pair<String, Any?>>): Intent {
    val intent = Intent(ctx, clazz)
    if (params.isNotEmpty()) fillIntentArguments(intent, params)
    return intent
}

private fun fillIntentArguments(intent: Intent, params: Array<out Pair<String, Any?>>) {
    params.forEach {
        when (val value = it.second) {
            null            -> intent.putExtra(it.first, null as Serializable?)
            is Int          -> intent.putExtra(it.first, value)
            is Long         -> intent.putExtra(it.first, value)
            is CharSequence -> intent.putExtra(it.first, value)
            is String       -> intent.putExtra(it.first, value)
            is Float        -> intent.putExtra(it.first, value)
            is Double       -> intent.putExtra(it.first, value)
            is Char         -> intent.putExtra(it.first, value)
            is Short        -> intent.putExtra(it.first, value)
            is Boolean      -> intent.putExtra(it.first, value)
            is Serializable -> intent.putExtra(it.first, value)
            is Bundle       -> intent.putExtra(it.first, value)
            is Parcelable   -> intent.putExtra(it.first, value)
            is Array<*>     -> when {
                value.isArrayOf<CharSequence>() -> intent.putExtra(it.first, value)
                value.isArrayOf<String>()       -> intent.putExtra(it.first, value)
                value.isArrayOf<Parcelable>()   -> intent.putExtra(it.first, value)
                else                            -> throw IntentException("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
            }
            is IntArray     -> intent.putExtra(it.first, value)
            is LongArray    -> intent.putExtra(it.first, value)
            is FloatArray   -> intent.putExtra(it.first, value)
            is DoubleArray  -> intent.putExtra(it.first, value)
            is CharArray    -> intent.putExtra(it.first, value)
            is ShortArray   -> intent.putExtra(it.first, value)
            is BooleanArray -> intent.putExtra(it.first, value)
            else            -> throw IntentException("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
        }
        return@forEach
    }
}

open class IntentException(message: String = "") : RuntimeException(message)
